<template>
    <view class="blood-container">
        <CmsTabs :type="3" :page-title="'遗体（器官）捐献'" @project-tab-change="handleProjectTabChange" @data-loaded="handleDataLoaded">
            <view class="footer-btn">
                <view class="primary-btn" @click="handleGoRegister">我要登记</view>
            </view>
        </CmsTabs>

        <!-- 我要登记弹窗 -->
        <view class="register-popup" v-if="showRegisterPopup" @click="handleClosePopup">
            <view class="popup-mask"></view>
            <view class="popup-content" @click.stop>
                <view class="popup-header">
                    <view class="popup-title">捐献登记</view>
                    <view class="popup-close" @click="handleClosePopup">×</view>
                </view>
                <view class="popup-form">
                    <view class="form-item">
                        <view class="form-label">姓名</view>
                        <input class="form-input" v-model="formData.name" placeholder="请输入" />
                    </view>
                    <view class="form-item">
                        <view class="form-label">手机号</view>
                        <input class="form-input" v-model="formData.phone" placeholder="请输入" type="number" />
                    </view>
                    <view class="form-item">
                        <view class="form-label">身份证号</view>
                        <input class="form-input" v-model="formData.idCard" placeholder="请输入" />
                    </view>
                    <view class="form-item">
                        <view class="form-label">所在地区</view>
                        <view class="form-input-area">
                            <picker @change="handleRegionChange" :value="formData.regionIndex" :range="regionOptions">
                                <view class="picker-text">{{ formData.regionIndex >= 0 ? regionOptions[formData.regionIndex] : '请选择' }}</view>
                            </picker>
                            <image src="../../static/right.png" mode="widthFix" class="right-icon"></image>
                        </view>
                    </view>
                    <view class="form-item">
                        <view class="form-label">捐献意愿</view>
                        <view class="form-input-area">
                            <picker @change="handleDonationChange" :value="formData.donationIndex" :range="donationOptions">
                                <view class="picker-text">{{ formData.donationIndex >= 0 ? donationOptions[formData.donationIndex] : '请选择' }}</view>
                            </picker>
                            <image src="../../static/right.png" mode="widthFix" class="right-icon"></image>
                        </view>
                    </view>
                    <view class="form-item">
                        <view class="form-label">手机号</view>
                        <input class="form-input" v-model="formData.phone" placeholder="请输入" type="number" />
                    </view>
                    <view class="form-item">
                        <view class="form-label">验证码</view>
                        <view class="form-input-code">
                            <input class="form-input" v-model="formData.verifyCode" placeholder="请输入" />
                            <view class="code-btn" @click="handleGetCode">获取验证</view>
                        </view>
                    </view>
                </view>
                <view class="popup-footer">
                    <view class="primary-btn" @click="handleConfirmRegister">确认登记</view>
                </view>
            </view>
        </view>


        <view class="success-popup" v-if="showCopySuccessPopup" @click="handleCloseCopySuccessPopup">
            <view class="popup-mask"></view>
            <view class="success-popup-content" @click.stop>
                <view class="success-icon">
                    <image src="../../static/success2.png" mode="widthFix" />
                </view>
                <view class="success-title">登记地址已复制</view>
                <view class="success-message">请在手机自带浏览器的地址栏中长按，<br/>点粘贴并进入，即可进入登记平台。</view>
                <view class="browser-guide">
                    <image src="../../static/demo.png" mode="widthFix" />
                </view>
                <view class="success-btn" @click="handleCloseCopySuccessPopup">
                    <text class="btn-text">我知道了</text>
                </view>
            </view>
        </view>
    </view>
</template>

<script setup>
    import { ref } from 'vue'
    import CmsTabs from '@/components/cms-tabs/cms-tabs.vue'
    import { handleFormButtonClick, handleLoginRequiredPopup } from '@/utils/loginGuard.js'
    import { copyToClipboard } from '@/utils/copy.js'

    // 处理项目tab切换事件
    const handleProjectTabChange = (event) => {
        console.log('项目tab切换:', event)
    }

    // 处理数据加载完成事件
    const handleDataLoaded = (event) => {
        console.log('数据加载完成:', event)
    }

    // 弹窗控制
    const showRegisterPopup = ref(false)
    const showCopySuccessPopup = ref(false)
    
    // 表单数据
    const formData = ref({
        name: '',
        phone: '',
        idCard: '',
        region: '',
        regionText: '',
        donationIndex: 0,
        verifyCode: ''
    })

    // 地区选项
    const regionOptions = ref(['市中区', '薛城区', '峄城区', '台儿庄区', '山亭区', '滕州市'])

    // 捐献意愿选项
    const donationOptions = ref(['心脏', '肝脏', '肾脏', '肺脏', '胰腺', '小肠'])

    // 我要登记
    const handleGoRegister = async () => {
        // 直接跳转到器官捐献登记页面
        uni.navigateTo({
            url: '/pages/webview/webview?url=' + encodeURIComponent('https://register.codac.org.cn/front_terminal/zzhh_terminal/notice.html') + '&title=器官捐献登记'
        })
    }

    // 关闭弹窗
    const handleClosePopup = () => {
        showRegisterPopup.value = false
        // 重置表单数据
        formData.value = {
            name: '',
            phone: '',
            idCard: '',
            region: '',
            regionText: '',
            donationIndex: 0,
            verifyCode: ''
        }
    }

    // 确认登记
    const handleConfirmRegister = () => {
        // 表单验证
        if (!validateForm()) {
            return
        }
        
        // 这里可以添加表单验证逻辑
        console.log('登记信息：', formData.value)
        // 提交成功后关闭弹窗
        handleClosePopup()
    }

    // 表单验证
    const validateForm = () => {
        const { name, idCard, region, phone, verifyCode } = formData.value
        
        // 必填验证
        if (!name.trim()) {
            uni.showToast({
                title: '请输入姓名',
                icon: 'none'
            })
            return false
        }
        
        if (!idCard.trim()) {
            uni.showToast({
                title: '请输入身份证号',
                icon: 'none'
            })
            return false
        }
        
        if (!region || region.length === 0) {
            uni.showToast({
                title: '请选择居住地',
                icon: 'none'
            })
            return false
        }
        
        if (!phone.trim()) {
            uni.showToast({
                title: '请输入手机号',
                icon: 'none'
            })
            return false
        }
        
        if (!verifyCode.trim()) {
            uni.showToast({
                title: '请输入验证码',
                icon: 'none'
            })
            return false
        }
        
        // 身份证号格式验证
        if (!validateIdCard(idCard)) {
            uni.showToast({
                title: '身份证号格式不正确',
                icon: 'none'
            })
            return false
        }
        
        // 手机号格式验证
        if (!validatePhone(phone)) {
            uni.showToast({
                title: '手机号格式不正确',
                icon: 'none'
            })
            return false
        }
        
        return true
    }
    
    // 身份证号验证
    const validateIdCard = (idCard) => {
        // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
        const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
        return reg.test(idCard)
    }
    
    // 手机号验证
    const validatePhone = (phone) => {
        // 手机号格式验证：1开头，第二位是3-9，后面9位数字
        const reg = /^1[3-9]\d{9}$/
        return reg.test(phone)
    }

    // 处理地区选择
    const handleRegionChange = (e) => {
        const value = e.detail.value
        formData.value.region = value
        // 这里可以根据选择的地区更新地区文本
        formData.value.regionText = value.join(' ')
    }

    // 处理捐献意愿选择
    const handleDonationChange = (e) => {
        const value = e.detail.value
        formData.value.donationIndex = value
    }

    // 处理获取验证码
    const handleGetCode = () => {
        const { phone } = formData.value
        
        // 验证手机号
        if (!phone.trim()) {
            uni.showToast({
                title: '请输入手机号',
                icon: 'none'
            })
            return
        }
        
        if (!validatePhone(phone)) {
            uni.showToast({
                title: '手机号格式不正确',
                icon: 'none'
            })
            return
        }
        
        // 这里可以添加获取验证码的逻辑
        console.log('获取验证码，手机号：', phone)
        
        // 显示成功提示
        uni.showToast({
            title: '验证码已发送',
            icon: 'success'
        })
    }

    // 关闭复制成功弹窗
    const handleCloseCopySuccessPopup = () => {
        showCopySuccessPopup.value = false
    }
</script>

<style lang="scss" scoped>  
    .blood-container{
        width: 100%;
        min-height: 100vh;
        background: #f5f5f5;
        padding-bottom: 140rpx;
    }

    .footer-btn{
        position: fixed;
        bottom: 0;
        left: 0;
        width: calc(100% - 48rpx);
        height: 100rpx;
        background: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 24rpx 24rpx 28rpx 24rpx;
        z-index: 100;
    }

    // 登记弹窗样式
    .register-popup {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 999;
        display: flex;
        align-items: flex-end;

        .popup-mask {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
        }

        .popup-content {
            position: relative;
            width: 100%;
            max-height: 85vh;
            min-height: 500rpx;
            background: #FFFFFF;
            border-radius: 24rpx 24rpx 0rpx 0rpx;
            z-index: 1000;
            display: flex;
            flex-direction: column;

            .popup-header {
                display: flex;
                align-items: center;
                justify-content: space-between;
                padding: 32rpx 24rpx 24rpx 24rpx;
                position: relative;
                flex-shrink: 0;
                border-bottom: 2rpx solid #f0f0f0;

                .popup-title {
                    position: absolute;
                    left: 50%;
                    transform: translateX(-50%);
                    font-size: 32rpx;
                    line-height: 44rpx;
                    font-weight: 500;
                    color: #000;
                    text-align: center;
                }

                .popup-close {
                    width: 48rpx;
                    height: 48rpx;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    font-size: 40rpx;
                    color: rgba(0, 0, 0, 0.90);
                    cursor: pointer;
                    margin-left: auto;
                }
            }

            .popup-form {
                flex: 1;
                overflow-y: auto;
                padding: 0 24rpx;

                .form-item {
                    display: flex;
                    align-items: center;
                    padding: 24rpx 0;
                    border-bottom: 2rpx solid #E7E7E7;

                    &:last-child {
                        border-bottom: none;
                        padding-bottom: 32rpx;
                    }

                    &:first-child {
                        padding-top: 32rpx;
                    }

                    .form-label {
                        width: 160rpx;
                        font-size: 28rpx;
                        color: rgba(0, 0, 0, 0.60);
                        flex-shrink: 0;

                        .required {
                            color: #F9623E;
                            margin-left: 4rpx;
                        }
                    }

                    .form-input {
                        flex: 1;
                        text-align: right;
                        font-size: 28rpx;
                        border: none;
                        outline: none;
                        background: transparent;

                        &::placeholder {
                            color: rgba(0, 0, 0, 0.40);
                        }
                    }

                    .radio-group {
                        flex: 1;
                        display: flex;
                        justify-content: flex-end;
                        gap: 32rpx;

                        .radio-item {
                            display: flex;
                            align-items: center;
                            gap: 8rpx;
                            font-size: 28rpx;
                            color: rgba(0, 0, 0, 0.60);
                        }
                    }

                    .form-input-area {
                        flex: 1;
                        text-align: right;
                        display: flex;
                        align-items: center;
                        justify-content: flex-end;

                        .picker-text {
                            font-size: 28rpx;
                            color: rgba(0, 0, 0, 0.40);
                            margin-right: 8rpx;
                        }

                        .right-icon {
                            width: 24rpx;
                            height: 24rpx;
                            flex-shrink: 0;
                        }
                    }
                }
            }

            .popup-footer {
                flex-shrink: 0;
                padding: 24rpx;
                border-top: 2rpx solid #E7E8EB;
                background: #FFFFFF;

                .primary-btn {
                    width: 100%;
                    height: 88rpx;
                    background: linear-gradient(135deg, #F9623E 0%, #FF7A5A 100%);
                    border-radius: 44rpx;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    color: #FFFFFF;
                    font-size: 32rpx;
                    font-weight: 500;
                    box-shadow: 0 8rpx 24rpx rgba(249, 98, 62, 0.3);
                    transition: all 0.3s ease;

                    &:active {
                        transform: scale(0.98);
                        box-shadow: 0 4rpx 12rpx rgba(249, 98, 62, 0.3);
                    }
                }
            }
        }
    }

    // 复制成功弹窗样式
    .copy-success-popup {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 1000;
        display: flex;
        align-items: center;
        justify-content: center;

        .popup-mask {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
        }

        .copy-success-content {
            position: relative;
            width: 600rpx;
            min-height: 500rpx;
            background: #FFFFFF;
            border-radius: 24rpx;
            z-index: 1001;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 40rpx 32rpx;

            .success-icon {
                width: 120rpx;
                height: 120rpx;
                margin-bottom: 24rpx;

                image {
                    width: 100%;
                    height: 100%;
                }
            }

            .success-title {
                font-size: 36rpx;
                font-weight: 600;
                color: #000;
                margin-bottom: 16rpx;
                text-align: center;
            }

            .success-message {
                font-size: 28rpx;
                color: rgba(0, 0, 0, 0.80);
                text-align: center;
                margin-bottom: 32rpx;
                line-height: 1.5;
            }

            .browser-guide {
                margin-bottom: 40rpx;

                .phone-frame {
                    position: relative;
                    width: 200rpx;
                    height: 300rpx;
                    background: #f5f5f5;
                    border-radius: 20rpx;
                    display: flex;
                    flex-direction: column;
                    align-items: center;
                    padding: 20rpx;

                    .browser-icon {
                        width: 60rpx;
                        height: 60rpx;
                        background: #007AFF;
                        border-radius: 12rpx;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        margin-bottom: 20rpx;
                        position: relative;

                        .browser-label {
                            font-size: 20rpx;
                            color: #fff;
                            text-align: center;
                        }

                        &.browser-icon-1 {
                            margin-top: 20rpx;
                        }

                        &.browser-icon-2 {
                            margin-top: 40rpx;
                        }
                    }

                    .highlight-box {
                        position: absolute;
                        top: 40rpx;
                        left: 20rpx;
                        width: 60rpx;
                        height: 60rpx;
                        border: 4rpx solid #FF3B30;
                        border-radius: 12rpx;
                    }

                    .highlight-arrow {
                        position: absolute;
                        top: 70rpx;
                        left: 90rpx;
                        width: 0;
                        height: 0;
                        border-left: 20rpx solid #FF3B30;
                        border-top: 10rpx solid transparent;
                        border-bottom: 10rpx solid transparent;
                    }
                }
            }

            .success-btn {
                width: 100%;
                height: 88rpx;
                background: linear-gradient(135deg, #F9623E 0%, #FF7A5A 100%);
                border-radius: 44rpx;
                display: flex;
                align-items: center;
                justify-content: center;

                .btn-text {
                    font-size: 32rpx;
                    font-weight: 500;
                    color: #FFFFFF;
                }
            }
        }
    }

    .loading {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 999;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(0, 0, 0, 0.5);
        text-align: center;
        font-size: 32rpx;
        color: #fff;
    }
</style>